package cn.kgc.service;

import cn.kgc.annotation.ZhuangPei;
import cn.kgc.dao.UserDao;
import cn.kgc.dao.UserDaoImpl;
import cn.kgc.pojo.User;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserServiceImpl implements  UserService {
    //@ZhuangPei
    private UserDao userDao;//ioc/di    aop

    public void setUserDao(UserDao userDao){
        this.userDao=userDao;
    }

    @Override
    public boolean addUser(User user) {
        System.out.println("业务层调用持久层进行添加操作");
        return userDao.insertUser(user)>0;
    }

    //用构造去模拟 spring初始化
        public UserServiceImpl() throws Exception {
                //1  读取配置文件
            SAXReader sd=new SAXReader();
            Document document=sd.read(this.getClass().getClassLoader().getResourceAsStream("mybeans.xml"));
             Element root= document.getRootElement();
             List<Element> jds=  root.elements();
              Map<String,Object> context=new HashMap<String,Object>();
          for (Element e:jds) {
              if(!e.attributeValue("leiming").equals(this.getClass().getName())){
                  String key= e.attributeValue("wy");
                  Object value= Class.forName(e.attributeValue("leiming")).newInstance();
                  context.put(key,value );
                  System.out.println(e.attributeValue("wy")+"=="+e.attributeValue("leiming"));
              }

            }
                //2  创建对象
                //3 保存对象到Context上下文(map)容器
                //4 将程序需要的对象从容器中取出并注入到程序中
                            //4.1 看字段上有没有指定的注解
                            //4.2 看方法上有没有指定的注解
                        //如果有就注入
            Class c1=this.getClass();
            Field[] fs=c1.getDeclaredFields();
            Set<String> keys=context.keySet();
            for (Field f:fs) {
                if (f.isAnnotationPresent(ZhuangPei.class)) {
                    f.setAccessible(true);
                    for (String key:keys) {
                        if(key.equals(f.getName())) {
                            f.set(this, context.get(key));
                        }
                    }

                }
            }
            Method[] ms=c1.getMethods();

            for (Method f:ms) {
                if (f.isAnnotationPresent(ZhuangPei.class)) {
                    for (String key:keys) {
                        System.out.println(key);
                        System.out.println(f.getName());
                        if(f.getName().toLowerCase().contains(key.toLowerCase())){
                            System.out.println("zxl");
                            f.invoke(this,context.get(key));
                        }
                    }

                }
            }

        }


}
